﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Input;
using ScrumTable.Common.SysLibs;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.Win32Interopt.Interop;
using Point = System.Windows.Point;

#endregion

namespace ScrumTable.UI.View.WinTouch.Util
{

    /// <summary>
    /// DropDropUtil Util
    /// </summary>
    public class KeyboardUtil
    {


        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

        private const UInt32 WM_SYSCOMMAND = 0x112;
        private const UInt32 SC_RESTORE = 0xf120;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);

        private const string OnScreenKeyboadApplication = "osk.exe";



        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
       
        /// <summary>
        /// Shows the Keyboard and moves the keyboard to the control
        /// </summary>
        /// <returns></returns>
        public static void StartKeyBoard(FrameworkElement relativeTo)
        {
            // Get the name of the On screen keyboard
            string processName = System.IO.Path.GetFileNameWithoutExtension(OnScreenKeyboadApplication);

            // Check whether the application is not running 
            var query = from process in Process.GetProcesses()
                        where process.ProcessName == processName
                        select process;

            var keyboardProcess = query.FirstOrDefault();

            // launch it if it doesn't exist
            if (keyboardProcess == null)
            {
                IntPtr ptr = new IntPtr(); ;
                bool sucessfullyDisabledWow64Redirect = false;

                // Disable x64 directory virtualization if we're on x64,
                // otherwise keyboard launch will fail.
                if (System.Environment.Is64BitOperatingSystem)
                {
                    sucessfullyDisabledWow64Redirect = Wow64DisableWow64FsRedirection(ref ptr);
                }

                // osk.exe is in windows/system folder. So we can directky call it without path
                using (Process osk = new Process())
                {
                    osk.StartInfo.FileName = OnScreenKeyboadApplication;
                    osk.Start();
                    osk.WaitForInputIdle(2000);
                }
                
                // Re-enable directory virtualisation if it was disabled.
                if (Environment.Is64BitOperatingSystem)
                    if (sucessfullyDisabledWow64Redirect)
                        Wow64RevertWow64FsRedirection(ptr);
             //   SetPos(relativeTo, handle);
            }
            else
            {
                // Bring keyboard to the front if it's already running
                var windowHandle = keyboardProcess.MainWindowHandle;
                SendMessage(windowHandle, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
            }

        }
        /*
        private static void SetPos(FrameworkElement relativeTo, IntPtr windowHandle)
        {
            var point = relativeTo.PointToScreen(new Point(0, relativeTo.ActualHeight));
            User32Util.RECT rect;
            User32Util.GetWindowRect(windowHandle, out rect);
            int width = Math.Abs(rect.Right - rect.Left);
            int height = Math.Abs(rect.Top - rect.Bottom);

            if (Application.Current.MainWindow.ActualHeight - point.Y - height > 0 && Application.Current.MainWindow.ActualWidth - point.X - width > 0)
            {
                User32Util.SetWindowPos(windowHandle, 0, (int) point.X, (int) point.Y, 0, 0, User32Util.SWP.NOSIZE | User32Util.SWP.NOZORDER);
            }
            else if (point.X - width > 0)
            {
                User32Util.SetWindowPos(windowHandle, 0, (int) (point.X - width), (int) (point.Y - height), 0, 0, User32Util.SWP.NOSIZE | User32Util.SWP.NOZORDER);
            }
            else
            {
                User32Util.SetWindowPos(windowHandle, 0, (int) (point.X + relativeTo.ActualWidth), (int) (point.Y - height), 0, 0, User32Util.SWP.NOSIZE | User32Util.SWP.NOZORDER);
            }
        }*/
        #endregion
    }
}
